//
//  PopViewAnimation.swift
//  ZHFWB
//
//  Created by 张海峰 on 2017/2/28.
//  Copyright © 2017年 张海峰. All rights reserved.
//

import UIKit

class PopViewAnimation: NSObject {
    //MARK:- 对外提供的属性
    var isPresented :Bool = false
    var presentedFrame: CGRect = CGRect.zero
    //可选类型的闭包
    var callBack :((_ presented : Bool) -> ())?
    //MARK:- 自定义构造函数 先对原先构造函数重写，如果不重写，默认构造函数会被覆盖。
    override init()
    {
        
    }
    init(callBack :@escaping (_ presented : Bool) -> ()) {
        self.callBack = callBack
    }
    
}
//MARK:- 自定义转场代理方法
extension PopViewAnimation: UIViewControllerTransitioningDelegate
{
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        //自定义转场动画的尺寸
        let  presention =  ZHFPresentationController(presentedViewController: presented, presenting: presenting)
        presention.presentedFrame = presentedFrame
        return presention
    }
    //目的: 自定义弹出动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        callBack!(isPresented)
        return self
    }
    //目的: 自定义消失动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        callBack!(isPresented)
        return self
    }
}
extension PopViewAnimation: UIViewControllerAnimatedTransitioning
{
    //动画执行时间
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    //获取转场的上下文，可以通过转场上下文获取弹出的View和消失的View
    //UITransitionContextViewKey.from :获取消失的View
    //UITransitionContextViewKey.to :获取弹出的View
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        //三目运算，如果isPresented = false 调用animationForPresentedView 否则调用 animationForDismissedView
        isPresented ?  animationForPresentedView(transitionContext: transitionContext) :animationForDismissedView(transitionContext: transitionContext)
    }
    func animationForPresentedView(transitionContext: UIViewControllerContextTransitioning)  {
        // 1.获取弹出的View
        let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
        //2.将弹出的View添加到containerView中
        transitionContext.containerView.addSubview(presentedView)
        //3.执行动画
        presentedView.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.0)
        //设置锚点
        presentedView.layer.anchorPoint = CGPoint.init(x: 0.5, y: 0)
        // 下面self.transitionDuration(using: transitionContext)就是调用该方法确定延迟时间
        UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {
            () ->Void in presentedView.transform = CGAffineTransform.identity
        }) { (_) -> Void in
            //必须告诉转场上下文你已经完成了动画
            transitionContext.completeTransition(true)
        }
    }
    func animationForDismissedView(transitionContext: UIViewControllerContextTransitioning)  {
        //获取消失的View
        let dismissView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
        UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {
            () ->Void in
            dismissView.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.001)
        }) { (_) -> Void in
            dismissView.removeFromSuperview()
            //必须告诉转场上下文你已经完成了动画
            transitionContext.completeTransition(true)
        }
}
}
